<?php

/*
 * 商家模型
 * @author wyb
 */

namespace Qwadmin\Model;

class W3ApiModel extends ApiModel {

    //自动组合入库数据 微三云
    public function getw3catlist($indata) {
     $max_execution_time =0;


        $params = array('customer_id' => 43);


        // $params['sign'] = self::get_sign($params, $token);
        $url = $this->url;

        $result = json_decode($this->post($url, $params));
        
        // var_dump($result);
        if ($result->rsp == 'succ') {
            $modelsType = M('shop_types');
            foreach ($result->data as $k => $v) {
                //var_dump($result->data);die();
                $catdata = array();
                $catdata['shop_sites_id'] = $this->api['shop_id'];
                $catdata['name'] = $v->cat_name;
                $catdata['shop_type_id'] = $v->shop_type_id;
                $catdata['pid'] = $v->pid;
                $catdata['path'] = '';

                $where['shop_sites_id'] = $this->api['shop_id'];
                $where['shop_type_id'] = $v->shop_type_id;

                $id = $modelsType->where($where)->getField("id");
                if ($id) {
                   $modelsType->where(array('id' => $id))->save($catdata);
                } else {
                    $modelsType->data($catdata)->add();
                }
            }
            
            return json_encode(array(array('rsp' => 'succ')));
        }
    }

    //自动组合入库数据 微三云
    public function getw3hhrcatlist($indata) {



        $params = array('customer_id' => 51);

        // $params['sign'] = self::get_sign($params, $token);
        $url = $this->url;

        $result = json_decode($this->post($url, $params));

        // var_dump($result);
//echo $this->api['shop_id'];
        if ($result->rsp == 'succ') {
            $modelsType = M('shop_types');
            foreach ($result->data as $k => $v) {
                //var_dump($result->data);die();
                $catdata = array();
                $catdata['shop_sites_id'] = $this->api['shop_id'];
                $catdata['name'] = $v->cat_name;
                $catdata['shop_type_id'] = $v->shop_type_id;
                $catdata['pid'] = $v->pid;
                $catdata['path'] = '';

                $where['shop_sites_id'] = $this->api['shop_id'];
                $where['shop_type_id'] = $v->shop_type_id;

                $id = $modelsType->where($where)->getField("id");
//var_dump($catdata);
                if ($id) {
                    $modelsType->where(array('id' => $id))->save($catdata);
                } else {
                    $modelsType->data($catdata)->add();
                }
            }
            return json_encode(array(array('rsp' => 'succ')));
        }
    }

    public function addnewgoods($goods_id, $shop_id, $store_id,$model) {
        ///$goods=new GoodsModel($goods_id);
        /*
          $goods_id='P0041417';
          $shop_id='10003';  ///partner_id
          $store_id='122';
         */
        //echo $goods_id;

 
        $partner = M('partner')->where(array('id' => $store_id))->find();
        $store = M('partner_store')->where(array(array('bind_partner' => $partner['id']), array('shop_id' => $shop_id)))->find();

        $goods = M('goods')->where(array('id' => $goods_id))->find();
        $goods_type = M('shop_types')->where(array('goods_type_id' => $goods['goods_type_id']))->find();



        if (!$goods){
            return array('rsp' => 'err', 'log' => 'No this goods id;', 'optime' => time());
        }
        // if (!$goods_type){
        //     return array('rsp' => 'err', 'log' => 'No this goods_type;' . $goods['goods_type_id'],'optime' => time());
        // }
        if (!$store){
            return array('rsp' => 'err', 'log' => 'No this store;','optime' => time());
        }


        $goods['store_id'] = $store['store_id'];
        $goods['goods_type_id'] = $goods_type['shop_type_id'];
        $goods['trade_type'] = $goods_type['trade_type'] ? $goods_type['trade_type'] : '1'; ///1是普通 2 进口 3直邮
        $goods['model'] = !empty($model) ? $model:'add';
        ///$goods['customer_id']=(int)$shop_id;
        $goods['customer_id'] = $this->get_customer_byshop_id($shop_id);

        $goods['weight'] = $goods['weight']/1000;

     
        // var_dump($goods);
        echo "<br>url:<br>" . $this->url . "<br>";
        ///var_dump($goods);
        //die();
        $result = json_decode($this->post($this->url, $goods));
        $rd['rsp'] = $result->status;
        $rd['log'] = $result->msg;
        $rd['optime'] = time();
        //$resultx=M('goods_op')->where(array(array('goods_id'=>$goods_id),array('site_id'=>$shop_id),array('store_id'=>$store_id)))->data($rd)->save();
        //var_dump($result);
        //echo "w3 result:";
        return $rd;
    }

    public function editGoods($goods_id, $shop_id, $store_id){

       return $this->addnewgoods($goods_id, $shop_id, $store_id,'edit');
    }

    public function goods_update($goods_id, $shop_id, $store_id, $type='') {
         $returns = array("rsp" => 'error', "log" => '', 'optime' => time());
        if($type == ''){
             $returns['log'] = "无操作方法";
            return $returns;
        }
       
        $partner = M('partner')->where(array('id' => $store_id))->find();
        $store = M('partner_store')->where(array(array('bind_partner' => $partner['id']), array('shop_id' => $shop_id)))->find();

        $goods = M('goods')->where(array('id' => $goods_id))->find();
        if (!$goods) {
            $returns['log'] = "商品没找到";
            return $returns;
        }

        $params['weight'] = $goods['weight']/1000;
        if ($store['store_id']) {
            $params['is_supply_id'] = $store['store_id'];
        }
        $params['customer_id'] = $this->get_customer_byshop_id($shop_id);
        $params['foreign_mark'] = $goods['sku'];
        if($type == 'price'){ //价格
              $params['now_price'] = $goods['price'];  
			  $params['price_market'] = $goods['price_market'];
        }
        if($type == 'storenum'){ //库存
             $params['storenum'] = $goods['stock_num'];
        }
        if($type == 'online'){ //上架
             $params['isout'] = 0;
        }
        if($type == 'down'){ //下架
             $params['isout'] = 1;
        }
        if ($goods['die_time'] && !empty($goods['die_time']) && ($type != null)) { //过期时间
            $params['die_time'] = $goods['die_time'];
            $params['die_content'] = '本商品为临期商品，购买后概不退货。过期日期：';
        }
        $params['model'] = 'update';

        $str = $this->post($this->url, $params);
        $result = json_decode($str, true);
        if ($result['rsp'] == 'fail') {
            $returns['log'] = "失败 " . $result['res'];
            return $returns;
        } else {
            $returns['log'] = "成功 ";
            $returns['rsp'] = "succ";
            return $returns;
        }
    }

    //更新商品价格
    public function goods_price_update($goods_id, $shop_id, $store_id) {
        return $this->goods_update($goods_id, $shop_id, $store_id, 'price');
    }
    //更新商品库存
    public function goods_store_update($goods_id, $shop_id, $store_id) {
        return $this->goods_update($goods_id, $shop_id, $store_id, 'storenum');
    }
    //商品上架
    public function goodsOnline($goods_id, $shop_id, $store_id) {
        return $this->goods_update($goods_id, $shop_id, $store_id, 'online');
    }
    //商品下架
    public function goodsDown($goods_id, $shop_id, $store_id) {
        return $this->goods_update($goods_id, $shop_id, $store_id, 'down');
    }


    //自动组合入库数据 微三云 服务号
    public function get_w3_store($indata) {
        $customer_id = $this->get_customer_byshop_id($this->api['shop_id']);

        $params = array('customer_id' => $customer_id);
        $url = $this->api['url'];
        ///echo $this->api['url']."<br>";
        $result = json_decode($this->post($this->api['url'], $params));
        echo "当前修改：".$this->api['shop_id']." 站点合作店铺信息。<br/>";

        if ($result->status == 'succ') {

            if ($result->data) {


                $partaner_stMdoel = M("partner_store");
                $partaner_stMdoel->startTrans();
           /*           
    			   $delflag = $partaner_stMdoel->where(array('shop_id' => $this->api['shop_id']))->delete();
                if ($delflag === false) {
                    $partaner_stMdoel->rollback();
                    echo "删除数据失败";
                    die;
                }
				*/
                
                foreach ($result->data as $key => $value) {
                    $data = array();           
     				$data = array(
                        'name' => $value->name,
                        'store_id' => $value->id,
                        'shop_id' => $this->api['shop_id'],
                    );
					$re=$partaner_stMdoel->where(array(
                        'store_id' => $value->id,
                        'shop_id' => $this->api['shop_id'],
                    ))->find();
				 
					if(count($re)==0||$re==false){
						$partaner_stMdoel->add($data);
						echo "add a store:".$data['name']."<br/>";
					}else{
						$partaner_stMdoel->save($data);
						echo "modify a store:".$data['name']."<br/>";
					};
				 
					
                }
				
                    $partaner_stMdoel->commit();
                    echo '操作成功';
              
            } else {
                echo "no data!";
            }


            return json_encode(array(array('rsp' => 'succ')));
        }
    }

    function get_customer_byshop_id($shop_id) {
        $map = array(
            '10003' => "43",
            '10004' => "51",
        );
        $customer_id = $map[$shop_id];
        return $customer_id ? $customer_id : 43;
    }

}
